import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:kkhousekeep/widget/shadow_widget.dart';

class WorkOrderItem extends StatefulWidget {
  final double? marginTop;
  final double? marginBottom;
  final String time;
  final String address;
  final String repairType;
  final String urgent;
  final List<Widget>? actions;

  const WorkOrderItem(
      {super.key,
      this.marginTop,
      this.marginBottom,
      this.time = '',
      this.address = '',
      this.repairType = '',
      this.urgent = '',
      this.actions});

  @override
  State<StatefulWidget> createState() {
    return _WorkOrderItemState();
  }

  static buildPrimaryButton({
    VoidCallback? onTap,
    String text = '',
    Color? textColor = const Color(0xFF1774E0),
  }) {
    return buildButton(
      text: text,
      textColor: textColor,
      onTap: onTap,
    );
  }

  static buildButton({
    VoidCallback? onTap,
    String text = '',
    Color? textColor,
  }) {
    return _builderItemButton(
      text: text,
      textColor: textColor,
      onTap: onTap,
    );
  }

  static Widget _builderItemButton({
    VoidCallback? onTap,
    String text = '',
    Color? textColor,
  }) {
    textColor = textColor ?? const Color(0XFF6D6D6D);
    return InkWell(
      onTap: onTap,
      child: Container(
        // alignment: Alignment.center,
        padding: EdgeInsets.only(left: 40.w, right: 40.w, top: 4.h),
        height: 56.h,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(28.w),
          border: Border.fromBorderSide(
            BorderSide(
              color: textColor,
              width: 2.w,
            ),
          ),
        ),
        child: Text(
          text,
          textAlign: TextAlign.center,
          style: TextStyle(
            fontSize: 32.sp,
            color: textColor,
          ),
        ),
      ),
    );
  }
}

class _WorkOrderItemState extends State<WorkOrderItem> {
  var leftTextStyle = TextStyle(
    fontSize: 28.sp,
    color: const Color(0xFF6D6D6D),
  );
  var rightTextStyle = TextStyle(
    fontSize: 28.sp,
    color: const Color(0xFF333333),
  );

  @override
  Widget build(BuildContext context) {
    return ShadowWidget.fixedHeight(
      marginHorizontal: 32,
      marginTop: widget.marginTop ?? 0,
      marginBottom: widget.marginBottom ?? 0,
      paddingVertical: 16,
      backgroundColor: Colors.white,
      radius: 16,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
              padding: EdgeInsets.only(left: 24.w, bottom: 2.h, top: 12.h),
              child: Text(
                widget.time,
                style: TextStyle(
                  fontSize: 24.sp,
                  color: const Color(0XFF999999),
                ),
              )),
          _buildDetail(),
          (widget.actions != null)
              ? const Divider(
                  color: Color(0x1C1774E0),
                )
              : Container(),
          (widget.actions != null) ? buildActions() : Container()
        ],
      ),
    );
  }

  Widget _buildDetail() {
    return Padding(
        padding: EdgeInsets.only(left: 24.w, right: 24.w),
        child: Table(
          defaultVerticalAlignment: TableCellVerticalAlignment.middle,
          columnWidths: const {
            0: IntrinsicColumnWidth(),
            1: FlexColumnWidth(),
          },
          children: [
            TableRow(children: [
              buildFormTitle("报修地址"),
              buildFormContent(widget.address),
            ]),
            TableRow(children: [
              buildFormTitle("报修类型"),
              buildFormContent(widget.repairType),
            ]),
            TableRow(children: [
              buildFormTitle("紧急程度"),
              buildFormContent(widget.urgent,
                  textColor:
                      (widget.urgent == "一般") ? null : const Color(0xFFDD5045)),
            ]),
          ],
        ));
  }

  buildFormTitle(String title, {double? paddingTop, double? paddingBottom}) {
    return Padding(
      padding:
          EdgeInsets.only(top: paddingTop ?? 8.h, bottom: paddingBottom ?? 8.h),
      child: Text(
        title,
        style: TextStyle(
          fontSize: 28.sp,
          color: const Color(0xFF6D6D6D),
        ),
      ),
    );
  }

  buildFormContent(String content,
      {double? paddingTop, double? paddingBottom, Color? textColor}) {
    return Padding(
      padding: EdgeInsets.only(
          left: 72.w, top: paddingTop ?? 0, bottom: paddingBottom ?? 0),
      child: Text(
        content,
        textAlign: TextAlign.end,
        style: TextStyle(
            fontSize: 28.sp, color: textColor ?? const Color(0xFF333333)),
      ),
    );
  }

  buildActions() {
    return Padding(
      padding: EdgeInsets.only(right: 32.w),
      child: Align(
        alignment: Alignment.topRight,
        child: Wrap(
          spacing: 16.w,
          children: widget.actions!,
        ),
      ),
    );
  }
}
